package cn.mulanbay.admin.api.persistent.service;

import cn.mulanbay.admin.api.web.bean.request.auth.SystemFunctionTreeRequest;
import cn.mulanbay.common.bean.PageSearch;
import cn.mulanbay.common.config.Constants;
import cn.mulanbay.persistent.dto.SystemFunctionDto;
import cn.mulanbay.persistent.entity.SystemFunction;
import cn.mulanbay.persistent.mapper.SystemFunctionMapper;
import cn.mulanbay.persistent.query.QueryBuilder;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class SystemFunctionService extends ServiceImpl<SystemFunctionMapper, SystemFunction> {

    @Autowired
    SystemFunctionMapper systemFunctionMapper;

    /**
     * 功能点菜单
     * @return
     */
    public List<SystemFunctionDto> getSystemFunctionMenu(){
        return systemFunctionMapper.getSystemFunctionMenu();
    }

    /**
     * 获取列表
     * @param cts
     * @return
     */
    public List<SystemFunction> getSystemFunctionList(SystemFunctionTreeRequest cts){
        QueryWrapper qw = QueryBuilder.build(cts);
        return baseMapper.selectList(qw);
    }

    /**
     * 获取列表
     *
     * @param us
     * @return
     */
    public Page<SystemFunction> getDataList(PageSearch us){
        Page<SystemFunction> pp = new Page<>(us.getPage(),us.getPageSize());
        QueryWrapper qw = QueryBuilder.build(us);
        pp.addOrder(OrderItem.asc("pid"));
        pp.addOrder(OrderItem.asc("order_index"));
        if(us.getPage()<= Constants.NO_PAGE){
            List<SystemFunction> list = baseMapper.selectList(qw);
            pp.setRecords(list);
        }else{
            pp = baseMapper.selectPage(pp,qw);
        }
        return pp;
    }

    /**
     * 获取角色功能点的菜单列表
     * @param roleId
     * @param visible
     * @return
     */
    public List<SystemFunction> getRoleFunctionMenuList(Long roleId, Boolean visible){
        return systemFunctionMapper.getRoleFunctionMenuList(roleId,visible);
    }

    /**
     * 删除功能点
     * @param rootId
     */
    public void deleteFunctions(Long rootId){
        systemFunctionMapper.deleteFunctions(rootId);
    }

}
